<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="40%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="养殖设施" prop="facilityId">
                <el-select class="w100p" v-model="ruleForm.facilityId" clearable filterable placeholder="请选择设施名称"
                    @change="getFacilityName">
                    <el-option v-for="(item, index) in facilityList" :key="index" :label="item.facilityName"
                        :value="item.facilityId" />
                </el-select>
            </el-form-item>
            <el-form-item label="预警条件" prop="filterType">
                <el-select v-model="ruleForm.filterType" class="w100p" clearable filterable placeholder="请选择预警条件选择"
                    @change="handleChangeFilterType">
                    <el-option label="天数" value="DAY"></el-option>
                    <el-option label="日期" value="DATE"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="条件配置" prop="filterCondition" v-if="ruleForm.filterType == 'DATE'">
                <el-select v-model="ruleForm.filterCondition" class="w100p" clearable filterable placeholder="请选择条件配置"
                    @change="handleChangeFilterCondition">
                    <el-option label="每周" value="WEEKLY"></el-option>
                    <el-option label="每月" value="MONTHLY"></el-option>
                    <el-option label="每年" value="ANNUALLY"></el-option>
                </el-select>
            </el-form-item>
            <!-- 天数 -->
            <el-form-item label="条件(封塘后)" prop="filterValue" v-if="ruleForm.filterType == 'DAY'">
                <el-input type="number" v-model="ruleForm.filterValue" placeholder="请输入参数">
                    <template slot="append">天</template>
                </el-input>
            </el-form-item>
            <!-- 日期 -->
            <el-form-item v-if="ruleForm.filterType == 'DATE'">
                <el-row>
                    <el-col :span="12">
                        <!-- -->
                        <el-form-item prop="filterSupply" v-if="ruleForm.filterCondition == 'ANNUALLY'">
                            <el-select v-model="ruleForm.filterSupply" class="w100p" clearable filterable
                                placeholder="请选择月" @change="getDayNum(ruleForm.filterSupply)">
                                <el-option v-for="m, i in 12" :key="i" :label="(m) + '月'" :value="m"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item prop="filterValue" v-if="ruleForm.filterCondition == 'MONTHLY'">
                            <el-select v-model="ruleForm.filterValue" class="w100p" clearable filterable
                                placeholder="请选择日"> 
                                <el-option v-for="d, i in dayNum" :key="i" :label="(d) + '日'" :value="d"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item prop="filterValue" v-if="ruleForm.filterCondition == 'ANNUALLY'">
                            <el-select v-model="ruleForm.filterValue" class="w100p" clearable filterable
                                placeholder="请选择日">
                                <el-option v-for="d, i in dayNum" :key="i" :label="(d) + '日'" :value="d"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item prop="filterValue" v-if="ruleForm.filterCondition == 'WEEKLY'">
                            <el-select v-model="ruleForm.filterValue" class="w100p" clearable filterable
                                placeholder="请选择周">
                                <el-option label="星期日" :value="1"></el-option>
                                <el-option label="星期一" :value="2"></el-option>
                                <el-option label="星期二" :value="3"></el-option>
                                <el-option label="星期三" :value="4"></el-option>
                                <el-option label="星期四" :value="5"></el-option>
                                <el-option label="星期五" :value="6"></el-option>
                                <el-option label="星期六" :value="7"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form-item>
            <!-- 周期 -->
            <!-- <el-form-item v-if="ruleForm.filterType == 'CYCLE'" prop="item6">
                <el-select v-model="ruleForm.item6" class="w100p" clearable filterable placeholder="选择周期类型">
                    <el-option label="年" value="年"></el-option>
                    <el-option label="月" value="月"></el-option>
                    <el-option label="日" value="日"></el-option>
                </el-select>
            </el-form-item> -->
            <!-- <el-form-item v-if="ruleForm.filterType == 'CYCLE'" prop="filterValue">
                <el-input type="number" v-model="ruleForm.filterValue" placeholder="请输入执行次数">
                    <template slot="append">次</template>
                </el-input>
            </el-form-item> -->
            <el-form-item label="使用疫苗" prop="materialNo">
                <el-select v-model="ruleForm.materialNo" class="w100p" clearable filterable placeholder="请选择疫苗"
                    @change="querymaterialName">
                    <el-option
                        v-if="item.materialTypeNo == '1077222993386450944' || item.materialTypeNo == '4165464165465146516'"
                        v-for="item, index in materialList" :key="index" :label="item.materialName"
                        :value="item.materialNo"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import earlyWarningConfiguration from '@/api/earlyWarningConfiguration'
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        facilityList: {
            type: Array,
            default: () => {
                return []
            }
        },
        materialList: {
            type: Array,
            default: () => {
                return []
            }
        },
        type: {
            type: String,
            default: () => {
                return ''
            }
        },
    },
    data() {
        let validatorNumber = function (rule, value, callback) {
            if (value > 0 && value % 1 == 0) {
                callback()
            } else {
                callback(new Error('请完善数据！'))
            }
        }
        return {
            filterConditionList: [
                {
                    name: "每年",
                    key: 'ANNUALLY'
                },
                {
                    name: "周期",
                    key: 'CYCLE'
                },
                {
                    name: "每月",
                    key: 'MONTHLY'
                },
                {
                    name: "每周",
                    key: 'WEEKLY'
                }
            ],
            dayNum: 31,
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            rules: {
                facilityId: [
                    { required: true, message: '请选择设施', trigger: 'change' }
                ],
                filterValue: [
                    { required: true, validator: validatorNumber, trigger: 'change' }
                ],
                filterType: [
                    { required: true, message: '请选择条件', trigger: 'change' }
                ],
                filterCondition: [
                    { required: true, message: '请选择条件', trigger: 'change' }
                ],
                materialNo: [
                    { required: true, message: '请选择药品', trigger: 'change' }
                ],
                filterSupply: [
                    { required: true, message: '请完善信息', trigger: 'change' }
                ],
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = earlyWarningConfiguration.add
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = earlyWarningConfiguration.edit
                this.addEditName = '编辑'
            }
        },
        getDayNum(e) {
            let day = Number(e) || 0;
            let maxM = [1, 3, 5, 7, 8, 12];
            let mixM = [2, 4, 6, 9, 11];
            if (day == 2) {
                this.dayNum = 28
            } else if (maxM.includes(day)) {
                this.dayNum = 31
            } else if (mixM.includes(day)) {
                this.dayNum = 30
            } else {
                this.dayNum = 31
            }
        },
        handleChangeFilterCondition() {
            this.$set(this.ruleForm, 'filterSupply', "");
            this.$set(this.ruleForm, 'filterValue', "");
        },
        handleChangeFilterType(e) {
            this.$set(this.ruleForm, 'filterCondition', "");
        },
        querymaterialName(no) {
            let result = this.materialList.find(obj => obj.materialNo == no);
            result.materialNo && this.$set(this.ruleForm, 'materialName', result.materialName)
        },
        getFacilityName() {
            let result = this.facilityList.find(obj => obj.facilityId == this.ruleForm.facilityId);
            this.$set(this.ruleForm, 'facilityName', result.facilityName || "");
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm, type: this.type }).then(res => {
                        this.cancel();
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.code == 1 ? '操作成功' : res.msg || '操作失败')
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
</script>
<style lang="scss" scoped></style>